package com.zx.zxpicture.shared.websocket.disruptor;

import cn.hutool.json.JSONUtil;
import com.lmax.disruptor.WorkHandler;
import com.zx.zxpicture.shared.websocket.model.PictureEditResponseMessage;
import com.zx.zxpicture.shared.websocket.PictureEditHandler;
import com.zx.zxpicture.shared.websocket.enums.PictureEditMessageTypeEnum;
import com.zx.zxpicture.shared.websocket.model.PictureEditRequestMessage;
import com.zx.zxpicture.domain.user.entity.User;
import com.zx.zxpicture.application.service.UserApplicationService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;

/**
 * 消费者
 * 处理disruptor队列中的事件
 */
@Component
public class PictureEditEventWorkHandler implements WorkHandler<PictureEditEvent> {
    @Resource
    @Lazy
    private PictureEditHandler pictureEditHandler;
    @Resource
    private UserApplicationService userApplicationService;
    /**
     * 自定义处理事件逻辑
     * @param pictureEditEvent 处理的事件
     * @throws Exception
     */
    @Override
    public void onEvent(PictureEditEvent pictureEditEvent) throws Exception {
        PictureEditRequestMessage pictureEditRequestMessage = pictureEditEvent.getPictureEditRequestMessage();
        //获取用户wsSession
        WebSocketSession session = pictureEditEvent.getSession();
        //从ws会话中拿到发送消息的用户信息和图片id
        User user = (User)session.getAttributes().get("user");
        Long pictureId = (Long)session.getAttributes().get("pictureId");
        //拿到操作的类型
        String type = pictureEditRequestMessage.getType();
        PictureEditMessageTypeEnum editMessageTypeEnum = PictureEditMessageTypeEnum.getEnumByValue(type);

        switch (editMessageTypeEnum){
            case ENTER_EDIT:
                pictureEditHandler.handleEnterEditMessage(pictureEditRequestMessage, session, user, pictureId);
                break;
            case EDIT_ACTION:
                pictureEditHandler.handleEditActionMessage(pictureEditRequestMessage, session, user, pictureId);
                break;
            case EXIT_EDIT:
                pictureEditHandler.handleExitEditMessage(pictureEditRequestMessage, session, user, pictureId);
                break;
            case EDIT_COMPLETE:
                pictureEditHandler.handleCompleteEditMessage(pictureEditRequestMessage, session, user, pictureId);
                break;
            default:
                PictureEditResponseMessage pictureEditResponseMessage = new PictureEditResponseMessage();
                pictureEditResponseMessage.setType(PictureEditMessageTypeEnum.ERROR.getText());
                pictureEditResponseMessage.setMessage("消息类型错误");
                pictureEditResponseMessage.setUser(userApplicationService.getUserVO(user));
                //这里没有自定义序列化器来将Long转换为String，因为是报错消息所以不需要
                if(session.isOpen()){
                    session.sendMessage(new TextMessage(JSONUtil.toJsonStr(pictureEditResponseMessage)));
                }
        }
    }
}
